import MySQLdb
import types
from MySqlDBHandle import MySqlDBHandle

class TagsRelationship():
    mSQLInsertStatement = "INSERT INTO rel_follower_tag(Follower_id, Tag_id, Relationship_Status, INSERT_TIMESTAMP, LAST_UPDATE_TIMESTAMP)\
     VALUES %S ON DUPLICATE KEY UPDATE LAST_UPDATE_TIMESTAMP = current_timestamp;"
    mSQLValueStatement = "(%s, %s, 1, current_timestamp, current_timestamp),"
    
    def __init__(self, iFollowerId, iTagsIdList):
        self.mFollowerId = iFollowerId
        self.mTagsIdList= iTagsIdList
        
    def _sendToDB(self):
        assert(type(self.mTagsIdList) == types.ListType)
        lValueStatement =""
        for lTagsId in  self.mTagsIdList:
            lValueStatement += (self.mSQLValueStatement % (self.mFollowerId, lTagsId))
        lSQLStatement = self.mSQLInsertStatement % lValueStatement[:len(lValueStatement) - 1]
        try:
            MySqlDBHandle.execSqlStatement(lSQLStatement)
        except Exception, e:
            print "Error When Insert Tags relationship of FollowerId = " % (self.mFollowerId)
    
    def process(self):
        if type(self.mTagsIdList) == types.ListType and len(self.mTagsIdList) > 0:
            self._sendToDB()
    
        